package com.chen.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Constants;
import com.chen.common.Result;
import com.chen.entity.MailLog;
import com.chen.entity.Menu;
import com.chen.entity.User;

import com.chen.mapper.MailLogMapper;
import com.chen.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenchao
 * @date 2022/7/21 11:05
 */
@RestController
@RequestMapping("/test")
@Api(value = "TestController", tags = "测试")
public class TestController {

    @Resource
    private UserMapper userMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource
    private MailLogMapper mailLogMapper;


    @GetMapping("/demo")
    @ApiOperation(value = "MongoDB分页")
    public Page<User> demo(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize,
                           @RequestParam(defaultValue = "") String username) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        Page<User> page = new Page<>(pageNum, pageSize);
        if (!"".equals(username)) {
            params.put("username", username);
        }
        Page<User> userPage = userMapper.findPage(page, params);
        List<User> records = page.getRecords();
        records.forEach(System.out::println);
        return userPage;
    }

    @GetMapping("/send")
    @ApiOperation(value = "发送邮件")
    public String sendMail() {
        User user = userMapper.selectById(3);
        String msgId = UUID.randomUUID().toString();
        MailLog mailLog = new MailLog();
        mailLog.setMsgId(msgId);
        mailLog.setEid(user.getId());
        mailLog.setStatus(0);
        mailLog.setRouterKey(Constants.MMAIL_ROUTING_KEY_NAME);
        mailLog.setExchange(Constants.MMAIL_EXCHANGE_NAME);
        mailLog.setCount(0);
        mailLog.setTryTime(LocalDateTime.now().plusMinutes(Constants.MSG_TIMEOUT));
        mailLog.setCreateTime(LocalDateTime.now());
        mailLog.setUpdateTime(LocalDateTime.now());
        mailLogMapper.insert(mailLog);
        rabbitTemplate.convertAndSend(Constants.MMAIL_EXCHANGE_NAME, Constants.MMAIL_ROUTING_KEY_NAME, user, new CorrelationData(msgId));
        return "邮件发送成功";
    }


    @GetMapping("/ddd")
    public int ddd() {
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        return loginIdAsInt;

    }

    /* QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
         queryWrapper.orderByDesc("sort_num");
         if (StrUtil.isNotBlank(name)) {
         queryWrapper.like("name", name);
     }
     // 查询所有数据
     List<Menu> list = list(queryWrapper);
     // 找出pid为null的一级菜单
     List<Menu> parentNodes = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
     // 找出一级菜单的子菜单
         for (Menu menu : parentNodes) {
         // 筛选所有数据中pid=父级id的数据就是二级菜单
         menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
     }
         return parentNodes;
 }*/
    @GetMapping("/selectUserDetail")
    public Result selectUserDetail(@RequestParam(defaultValue = "") Integer userId) {
        List<Map<String, Object>> map = new ArrayList<Map<String, Object>>();
        User user = new User();
        User user1 = userMapper.selectById(userId);
        List<Menu> menus = userMapper.selectUserDetail(userId);
        List<Menu> pareneNode = menus.stream().filter(m -> m.getPid() == null).collect(Collectors.toList());
        for (Menu menu : pareneNode) {
            menu.setChildren(menus.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }

        user.setMenus(pareneNode);
        user.setUsername(user1.getUsername());
        user.setNickname(user1.getNickname());
        return Result.success(user);


    }


/*    public static void main(String[] args) throws Exception {
        String PATH = "D:\\Users\\chenchao\\Desktop\\";
//        创建一个工作簿
//        Workbook work=new HSSFWorkbook();  //03xls
        Workbook work = new XSSFWorkbook();   //07 xlsx
//        创建一个工作表
        Sheet sheet = work.createSheet("鲨鱼不傻");
//        创建一个行
        Row row = sheet.createRow(0);
//        创建单元格
        Cell cell = row.createCell(0);
        cell.setCellValue("观看人数");
        Cell cell1 = row.createCell(1);
        cell1.setCellValue(666);


        Row row1 = sheet.createRow(1);
        Cell cell2 = row1.createCell(0);
        cell2.setCellValue("统计时间");
        Cell cell3 = row1.createCell(1);
        String s = new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell3.setCellValue(s);
        FileOutputStream outputStream = new FileOutputStream(PATH + "鲨鱼不傻.xls");
        work.write(outputStream);
        outputStream.close();


    }*/

    @GetMapping("/demo01")
    @ApiOperation(value = "测试map")
    public Result demo01(){
        List<Map<String, Object>> mapList=new ArrayList<Map<String, Object>>();
        Map<String, Object> map=new HashMap<String, Object>();
        Map<String, Object> map1=new HashMap<String, Object>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");
        map1.put("4","d");
        map1.put("5","e");
        map1.put("6","f");
        mapList.add(map);
        mapList.add(map1);
        return Result.success(mapList);
    }


}
